package com.example.administrator.szb.fragments.shoucang.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.ShouCangActivity;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;
import com.example.administrator.szb.fragments.shoucang.ItemsClickListener;
import com.example.administrator.szb.util.QTLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by stone on 2018/1/4.
 */

public class XMShouCangAdapter extends android.widget.BaseAdapter{
    private List list;
    private Context context;
    LayoutInflater inflater;
    public XMShouCangAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    private List selectList = new ArrayList();
    public void setSelectAll(){
        selectList.clear();
        for (int i=0;i<list.size();i++){
            selectList.add(list.get(i));
        }

        notifyDataSetChanged();
    }
    public void setSelectNo(){
        selectList.clear();
        notifyDataSetChanged();
    }

    public void showCheckBox(ViewHolder holder, final Object obj, View view, final int pos){
        view.setOnClickListener(null);
        holder.checkBox_ll.setVisibility(View.GONE);
        holder.my_check_box.setVisibility(View.GONE);

        if (ShouCangActivity.canShowCheckBox){
            holder.checkBox_ll.setVisibility(View.VISIBLE);
            if (selectList.contains(obj)){
                holder.my_check_box.setVisibility(View.VISIBLE);
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selectList.contains(obj)){
                        selectList.remove(obj);
                    }else {
                        selectList.add(obj);
                    }
                    notifyDataSetChanged();
                }
            });

        }else {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onItemClick(pos);
                    }
                }
            });
        }
    }
    ItemsClickListener listener=null;
    public void setOnItemClick(ItemsClickListener l){
        listener=l;
    }

    public List getSelectList(){
        return selectList;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_xiangmu,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.reset();
        holder.iv_supei.setVisibility(View.GONE);
        holder.clock.setVisibility(View.VISIBLE);
        Supeis supeis = (Supeis) getItem(position);
        holder.tv_title.setText(supeis.getTitle());
        holder.tips_content.setText(supeis.getEnd_time());
        holder.ll_bottom.setVisibility(View.VISIBLE);
        holder.center_content.setText(supeis.getOther_require());
        int n=supeis.getFirst().size();
        if (n>0){
            holder.ll_center1.setVisibility(View.VISIBLE);

            List<Map<String,String>> l = supeis.getFirst();
            for(int j=0;j<l.size();j++)
            {
                Map<String,String> map = l.get(j);
                if (j==0){
                    holder.tv_1.setVisibility(View.VISIBLE);
                    holder.tv_1_content.setVisibility(View.VISIBLE);
                    for(Map.Entry<String, String> entry:map.entrySet())
                    {
                        holder.tv_1.setText(entry.getValue());
                        holder.tv_1_content.setText(entry.getKey());
                    }
                }
                if (j==1){
                    holder.tv_2.setVisibility(View.VISIBLE);
                    holder.tv_2_content.setVisibility(View.VISIBLE);
                    for(Map.Entry<String, String> entry:map.entrySet())
                    {
                        holder.tv_2.setText(entry.getValue());
                        holder.tv_2_content.setText(entry.getKey());
                        QTLog.e(entry.getKey()+":"+entry.getKey());
                    }
                }
                if (j==2){
                    holder.tv_3.setVisibility(View.VISIBLE);
                    holder.tv_3_content.setVisibility(View.VISIBLE);
                    for(Map.Entry<String, String> entry:map.entrySet())
                    {
                        holder.tv_3.setText(entry.getValue());
                        holder.tv_3_content.setText(entry.getKey());
                        QTLog.e(entry.getKey()+":"+entry.getKey());
                    }
                }
            }


        }else {
            holder.ll_center2.setVisibility(View.VISIBLE);
        }
        if (supeis.getStatus()==0){
            holder.tv_status.setText("待审核");
            holder.tv_status.setTextColor(Color.parseColor("#0096FF"));
            holder.tv_status.setBackgroundColor(Color.parseColor("#D7EBFA"));
        }
        if (supeis.getStatus()==1){
            holder.tv_status.setText("进行中");
            holder.tv_status.setTextColor(Color.parseColor("#0096FF"));
            holder.tv_status.setBackgroundColor(Color.parseColor("#D7EBFA"));
        }
        if (supeis.getStatus()==2){
            holder.tv_status.setText("未通过");
            holder.tv_status.setTextColor(Color.parseColor("#ffffff"));
            holder.tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
        }
        if (supeis.getStatus()==3){
            holder.tv_status.setText("已结束");
            holder.tv_status.setTextColor(Color.parseColor("#ffffff"));
            holder.tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
        }
        showCheckBox(holder,supeis, convertView,position);
        return convertView;
    }

    class ViewHolder{
        View my_check_box;
        LinearLayout checkBox_ll;
        private LinearLayout ll_bottom;
        private ImageView iv_supei;
        private TextView tv_title;
        private TextView tv_status;
        private LinearLayout ll_center2;
        private TextView center_content;
        private LinearLayout ll_center1;
        private TextView tv_1;
        private TextView tv_2;
        private TextView tv_3;
        private TextView tv_1_content;
        private TextView tv_2_content;
        private TextView tv_3_content;
        private ImageView clock;
        private TextView tips;
        private TextView tips_content;




        public ViewHolder(View view) {
            my_check_box = view.findViewById(R.id.my_check_box);
            checkBox_ll = view.findViewById(R.id.checkBox_ll);
            checkBox_ll = (LinearLayout) view.findViewById(R.id.checkBox_ll);
            iv_supei = (ImageView) view.findViewById(R.id.iv_supei);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_status = (TextView) view.findViewById(R.id.tv_status);
            ll_center2 = (LinearLayout) view.findViewById(R.id.ll_center2);
            center_content = (TextView) view.findViewById(R.id.center_content);
            ll_center1 = (LinearLayout) view.findViewById(R.id.ll_center1);
            tv_1 = (TextView) view.findViewById(R.id.tv_1);
            tv_2 = (TextView) view.findViewById(R.id.tv_2);
            tv_3 = (TextView) view.findViewById(R.id.tv_3);
            tv_1_content = (TextView) view.findViewById(R.id.tv_1_content);
            tv_2_content = (TextView) view.findViewById(R.id.tv_2_content);
            tv_3_content = (TextView) view.findViewById(R.id.tv_3_content);
            clock = (ImageView) view.findViewById(R.id.clock);
            tips = (TextView) view.findViewById(R.id.tips);
            tips_content = (TextView) view.findViewById(R.id.tips_content);
            ll_bottom = view.findViewById(R.id.ll_bottom);
        }
        private void reset(){
            iv_supei.setVisibility(View.GONE);
            ll_center1.setVisibility(View.GONE);
            ll_center2.setVisibility(View.GONE);
            clock.setVisibility(View.GONE);
            ll_bottom.setVisibility(View.GONE);
            tv_1.setVisibility(View.GONE);
            tv_1_content.setVisibility(View.GONE);
            tv_2.setVisibility(View.GONE);
            tv_2_content.setVisibility(View.GONE);
            tv_3.setVisibility(View.GONE);
            tv_3_content.setVisibility(View.GONE);
            tv_status.setBackgroundColor(Color.parseColor("#A2A8B6"));
            tv_status.setTextColor(Color.parseColor("#ffffff"));
            tips.setText("项目结束时间：");
        }
    }

}
